package com.homedo.odin.solution.service.bizservice.mng;

import com.github.pagehelper.PageInfo;
import com.homedo.core.common.base.dto.Meta;
import com.homedo.core.common.exception.BizException;
import com.homedo.core.common.util.BeanUtils;
import com.homedo.odin.solution.contract.mng.BrandFirmDetailMngResp;
import com.homedo.odin.solution.contract.oms.BrandFirmListReq;
import com.homedo.odin.solution.contract.oms.BrandFirmListResp;
import com.homedo.odin.solution.contract.oms.UpdateBrandFirmReq;
import com.homedo.odin.solution.dao.BrandFirmMapper;
import com.homedo.odin.solution.dao.BrandOwnerMapper;
import com.homedo.odin.solution.enumcode.basic.ErrorCodeEnum;
import com.homedo.odin.solution.enumcode.solution.SolutionStateEnum;
import com.homedo.odin.solution.enumcode.solution.SolutionTypeEnum;
import com.homedo.odin.solution.enumcode.user.RoleEnum;
import com.homedo.odin.solution.model.dto.BrandFirmBean;
import com.homedo.odin.solution.model.dto.PageBean;
import com.homedo.odin.solution.model.entity.BrandFirm;
import com.homedo.odin.solution.model.entity.BrandOwner;
import com.homedo.odin.solution.model.entity.Solution;
import com.homedo.odin.solution.model.entity.UserProfile;
import com.homedo.odin.solution.service.BrandFirmServiceImpl;
import com.homedo.odin.solution.service.SolutionServiceImpl;
import com.homedo.odin.solution.service.UserProfileServiceImpl;
import java.util.Date;
import java.util.List;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author: donglibing
 * @date: 2018/3/27 17:36
 * @Description:
 */
@Service
public class MngBrandFirmBizService {


    @Autowired
    private BrandFirmServiceImpl brandFirmService;


    @Autowired
    private SolutionServiceImpl solutionService;

    @Autowired
    private UserProfileServiceImpl userProfileService;

    @Autowired
    BrandFirmMapper brandFirmMapper;

    @Autowired
    BrandOwnerMapper brandOwnerMapper;

    /**
     * OMS_获取品牌商列表
     */
    public BrandFirmListResp getBrandFirmList(BrandFirmListReq req) {
        PageBean pageBean = new PageBean(req.getPageNum(), req.getPageSize());
        PageInfo<BrandFirmBean> queryResult = brandFirmService
                .queryBrandFirmByParam(req.getUserId(), req.getStatus(), pageBean);
        return BeanUtils.transfrom(BrandFirmListResp.class, queryResult);
    }


    /**
     * 后台审核用户申请
     */
    @Transactional(rollbackFor = {RuntimeException.class, Exception.class})
    public void userBrandAudit(UpdateBrandFirmReq brandAuditBean, int userId) {

        if (!ArrayUtils.contains(
                new int[]{SolutionStateEnum.AUDITSUCCESS.getDbValue(), SolutionStateEnum.AUDITFAIL.getDbValue()},
                brandAuditBean.getStatus())) {
            throw new BizException(Meta.CODE_451.getCode(), "操作状态不正确！");
        }
        if (SolutionStateEnum.AUDITFAIL.getDbValue().equals(brandAuditBean.getStatus()) &&
                StringUtils.isEmpty(brandAuditBean.getAuditOpinion())) {
            throw new BizException(Meta.CODE_451.getCode(), "审核不通过时，审核意见不能为空!");
        }
        if (SolutionStateEnum.AUDITSUCCESS.getDbValue().equals(brandAuditBean.getStatus())) {
            brandAuditBean.setAuditOpinion(null);
        }
        BrandFirm brand = brandFirmService.getBrandFirmByUserId(userId);
        if (brand == null) {
            throw new BizException(Meta.CODE_451.getCode(), "该用户申请信息不存在!");
        }
        if (!ArrayUtils.contains(new int[]{SolutionStateEnum.PENDINGAUDIT.getDbValue()}, brand.getStatus())) {
            throw new BizException(Meta.CODE_451.getCode(), "当前状态不支持该操作!");
        }
        brand.setStatus(brandAuditBean.getStatus());
        brand.setAuditOpinion(brandAuditBean.getAuditOpinion());
        brandFirmService.updateById(brand);
        //如果审核通过则将该用户的方案更改为对应的品牌方案
        if (brandAuditBean.getStatus().equals(SolutionStateEnum.AUDITSUCCESS.getDbValue())) {
//查询是否存在该方案
            List<Solution> solutions = solutionService.findSolutionByUserId(userId);
            for (Solution solution : solutions) {
                solution.setBrandId(brand.getBrandId());
                solution.setSolutionType(SolutionTypeEnum.BRAND_SOLUTION.getDbValue());
                solution.setUserId(userId);
                solutionService.updateById(solution);
            }
        }
        //如果审核通过则,更新用户简介role为品牌主,更新图标为企业logo
        if (brandAuditBean.getStatus().equals(SolutionStateEnum.AUDITSUCCESS.getDbValue())) {
            UserProfile userProfile = userProfileService.findByUserId(userId);
            if (userProfile == null) {
                throw new BizException(Meta.CODE_451.getCode(), "未查询到该用户简介!");
            }
            userProfile.setRole(RoleEnum.BRANDOWNER.getDbValue());
            userProfile.setUserId(userId);
            userProfile.setIcon(brand.getFirmIcon());
            userProfile.setModifiedTime(new Date());
            userProfileService.updateById(userProfile);
        }
    }

    public BrandFirmDetailMngResp findBrandFirmByUserId(Integer userId) {
        BrandFirm brand = brandFirmMapper.getBrandFirm(userId);
        if (brand == null) {
            throw new BizException(ErrorCodeEnum.NotExist.getDbValue(), "该用户品牌商信息不存在");
        }
        BrandFirmDetailMngResp brandFirmResp = BeanUtils.transfrom(BrandFirmDetailMngResp.class, brand);
        UserProfile userProfile = userProfileService.findByUserId(userId);
        brandFirmResp.setUserName(userProfile.getUserName());
        brandFirmResp.setAddress(userProfile.getAddress());
        BrandOwner brandOwner = brandOwnerMapper.getBrandOwnerById(brand.getBrandId());
        if (brandOwner != null) {
            brandFirmResp.setBrandName(brandOwner.getBrandName());
        }
        return brandFirmResp;
    }
}
